package com.shop.front.controller.store;

import com.shop.domain.user.User;
import com.shop.front.consts.FrontConst;
import com.shop.front.service.user.UserService;
import com.shop.front.vo.index.ProductDetailIndexVo;
import com.shop.front.vo.store.StoreProductReplyCountVo;
import com.shop.front.vo.store.StoreProductVo;
import com.shop.domain.store.StoreProductAttr;
import com.shop.domain.store.StoreProductAttrValue;
import com.shop.domain.store.StoreProductDescription;
import com.shop.front.request.store.StoreProductReq;
import com.shop.front.service.store.StoreProductReplyService;
import com.shop.front.service.store.StoreProductService;
import com.shop.util.IdSignUtil;
import com.shop.web.cache.RedisCache;
import com.shop.web.context.WebContext;
import com.shop.web.response.BaseResponse;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@Api(description = "产品相关", position = -1)
@RestController
@RequestMapping(value = "/storeProduct", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
@Slf4j
public class StoreProductController {

    @Autowired
    private StoreProductService storeProductService;
    @Autowired
    private StoreProductReplyService storeProductReplyService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "列表")
    @ApiResponses(@ApiResponse(code = 10000, response = StoreProductVo.class, message = "content"))
    @RequestMapping("/page")
    public BaseResponse page(@RequestBody @Validated StoreProductReq req) {
        if(StringUtils.isNotBlank(req.getCateSign())) {
            Long cateId = IdSignUtil.checkSign(req.getCateSign());
            if(null == cateId) {
                return BaseResponse.fail("请选择正确的商品类型！");
            }
            req.setCateId(cateId);
        }
        if(StringUtils.isNotBlank(req.getPinkSign())) {
            Long pinkId = IdSignUtil.checkSign(req.getPinkSign());
            if(null == pinkId) {
                return BaseResponse.fail("请选择正确的拼团类型！");
            }
            req.setPinkId(pinkId);
        }

        return storeProductService.page(req);
    }

    @ApiOperation(value = "商品详情首页")
    @ApiResponses(@ApiResponse(code = 10000, response = ProductDetailIndexVo.class, message = "content"))
    @ApiImplicitParams(@ApiImplicitParam(name="sign", required = true, value = "商品id", paramType = "body"))
    @RequestMapping("/detailIndex")
    public BaseResponse detailIndex(@RequestBody @ApiIgnore Map<String, String> map) {
        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        ProductDetailIndexVo vo = null;

//        // 首页缓存key
//        int cacheTime = 600;  // 秒
//        String cacheKey = "detailIndex_" + id;
//        // 缓存是否存在
//        String cacheVal = redisCache.getString(cacheKey);
//        if(StringUtils.isNotBlank(cacheVal)) {
//            vo = JSON.parseObject(cacheVal, ProductDetailIndexVo.class);
//            return new BaseResponse(vo);
//        }

        // 商品基本信息
        StoreProductVo productVo = storeProductService.findVoById(id);

        // 商品详情
        StoreProductDescription productDescription = storeProductService.findDescriptionByProductId(id);
        String description = HtmlUtils.htmlUnescape(productDescription.getDescription());

        // 商品规格列表
        List<StoreProductAttr> productAttrList = storeProductService.productAttr(id);

        // 默认展示第一个规格产品
        String suk = "";
        String[] attrValues = null;
        for (StoreProductAttr productAttr : productAttrList) {
            attrValues = productAttr.getAttrValues().split(",");
            suk += attrValues[0] + ",";
        }
        suk = suk.substring(0, suk.length() - 1);
        // 查询产品指定的规格
        StoreProductAttrValue productAttrValue = storeProductService.productAttrValue(id, suk);

        // 商品评论数量、好评数、中评数、差评数、好评率
        StoreProductReplyCountVo productReplyCountVo = storeProductReplyService.count(id);

        vo = new ProductDetailIndexVo();
        vo.setId(id);
        vo.setProductVo(productVo);
        vo.setDescription(description);
        vo.setReplyCount(productReplyCountVo);
        vo.setProductAttrList(productAttrList);
        vo.setProductAttrValue(productAttrValue);

        User user = userService.authUserIdByAccessToken(WebContext.getAccessToken());
        vo.setIsCollect(userService.isCollect(user,id));

        // 缓存返回值
       redisCache.set(FrontConst.USER_PAY_SOTRE_ORDER+"HY787", "12", 60*30);

        return new BaseResponse(vo);
    }

    @ApiOperation(value = "商品规格详情")
    @ApiResponses(@ApiResponse(code = 10000, response = ProductDetailIndexVo.class, message = "content"))
    @ApiImplicitParams({@ApiImplicitParam(name="sign", required = true, value = "商品id", paramType = "body"),
            @ApiImplicitParam(name="suk", required = true, value = "商品规格组合名称", paramType = "body")})
    @RequestMapping("/attrValueBySuk")
    public BaseResponse attrValueBySuk(@RequestBody @ApiIgnore Map<String, String> map) {
        String suk = map.get("suk");
        if(StringUtils.isBlank(suk)) {
            return BaseResponse.fail("请选择商品规格");
        }

        Long id = WebContext.getSignId();
        if(null == id) {
            return BaseResponse.CHECK_SIGN_IS_FAIL;
        }

        StoreProductAttrValue productAttrValue = storeProductService.productAttrValue(id, suk);
        return new BaseResponse(productAttrValue);
    }

}
